<template>
  <div v-if="!showMessage" ref="item" class="item" :class="songId == listItem.id ? 'bgc' : ''">
    <div
      v-if="index != 0"
      class="item-index"
      :class="index <= 3 ? 'hot-red' : ''"
    >
      {{ index | numFilter }}
    </div>
    <div @click.stop="toListenHandler" class="item-left">
      <div class="title">
        <span class="title-first">{{ listItem.name }}</span
        >{{ listItem?.alias && showTitle ? listItem?.alias[0] : "" }}
      </div>
      <div class="little">
        <div class="img-sq"></div>
        <div class="my-text">
          <span v-for="(act, index) in listItem.artists" :key="act.id"
            >{{ act.name
            }}{{ index != listItem.artists.length - 1 ? "/" : "" }}</span
          >
          -
          {{
            listItem.album
              ? listItem.album.name
              : listItem?.ar
              ? listItem?.ar[0]?.name
                ? listItem?.ar[0]?.name
                : ""
              : ""
          }}
        </div>
      </div>
    </div>
    <div @click.stop="addToListenHandle" class="item-right">
      <div v-if="songId==listItem.id" :class="isStop?'paused':''" @click.stop="">
        <div></div>
        <div></div>
        <div></div>
      </div>
      <img v-else src="@/../public/images/002.png" alt="" />
    </div>
  </div>
  <div class="my-text item center" v-else>{{showMessage}}</div>
</template>

<script>
export default {
  name: "ListComponent",
  props: {
    listItem: {
      type: Object,
      default: () => ({}),
    },
    index: {
      type: Number,
      default: 0,
    },
    showTitle: {
      type: Boolean,
      default: true,
    },
    songId: {
      type: Number,
      default: -1,
    },
    isStop:{
      type:Boolean,
      default:true
    },
  },
  data() {
    return {
      isPlay: false,
      showMessage:""
    };
  },
  filters: {
    numFilter(val) {
      return val < 10 ? "0" + val : val;
    },
  },
  methods: {
    async toListenHandler() {
      let result = await this.axios.get(`/check/music?id=${this.listItem.id}`);
      if(result.data.success==false){
        console.log(result.data.message);
        this.showMessage = result.data.message;
        setTimeout(()=>this.showMessage="",2000);
        return;
      }
      this.$router.push(`/listen?id=${this.listItem.id}`);
    },
    async addToListenHandle() {
      let result = await this.axios.get(`/check/music?id=${this.listItem.id}`);
      if(result.data.success==false){
        console.log(result.data.message);
        this.showMessage = result.data.message;
        setTimeout(()=>this.showMessage="",2000);
        return;
      }
      localStorage.setItem("songId", this.listItem.id);
      localStorage.setItem("listItem", JSON.stringify(this.listItem));
      this.$emit("addToListenList");
    },
  },
  created() {
    // console.log(this.listItem);
  },
  mounted() {
    if (this.index == 0) this.$refs.item.classList.add("mg-left");
  },
  updated() {
    if (this.index == 0) this.$refs.item.classList.add("mg-left");
  },
};
</script>

<style lang="scss" scoped>
.mg-left {
  margin-left: 0.1rem;
}
.hot-red {
  color: #df3436 !important;
}
.img-sq {
  width: 0.12rem;
  min-width: 0.12rem;
  height: 0.08rem;
  margin-right: 0.04rem;
  background-image: url(@/../public/images/001.png);
  background-size: 100%;
  background-repeat: no-repeat;
}
.bgc {
  position: relative;
  &::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background-image: linear-gradient(
      to right,
      rgba(255, 255, 255, 0) 0%,
      rgba(223, 52, 54, 0.1) 10%,
      rgba(255, 255, 255, 0)
    );
    z-index: 0;
  }
}
.item {
  position: relative;
  padding-right: 0.1rem;
  box-sizing: border-box;
  height: 0.55rem;
  border-bottom: 0.01rem solid rgba($color: #ccc, $alpha: 0.2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
  >div{
    position: relative;
    z-index: 1;
  }
}
.center{
  justify-content: center;
}
.item-index {
  font-size: 0.17rem;
  width: 0.4rem;
  min-width: 0.4rem;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
}
.item-left {
  flex-grow: 1;
  font-size: 0.17rem;
  color: #888;
  .title {
    width: 100%;
    height: 0.26rem;
    line-height: 0.26rem;
    overflow: hidden;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    display: -webkit-box;
    .title-first {
      color: #333;
      margin-right: 0.04rem;
    }
  }
  .little {
    display: flex;
    align-items: center;
    width: 100%;
    font-size: 0.12rem;
    height: 0.18rem;
    > span {
      margin-left: 0.04rem;
      flex-grow: 1;
      overflow: hidden;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      display: -webkit-box;
    }
  }
}
.item-right {
  margin-left: 0.1rem;
  width: 0.22rem;
  min-width: 0.22rem;
  height: 0.22rem;
  overflow: hidden;
  .paused{
    >div{
      animation-play-state: paused !important;
    }
  }
  >div{
    width: 0.22rem;
    height: 0.22rem;
    display: flex;
    align-items: flex-end;
    justify-content: space-evenly;
    >div{
      width: 20%;
      height: 100%;
      background-color: #df3436;
      &:nth-child(1){
        animation: active-1 0.7s linear infinite;
        @keyframes active-1 {
          0%{
            height: 0;
          }50%{
            height: 80%;
          }100%{
            height: 0;
          }
        }
      }
      &:nth-child(2){
        animation: active-2 0.8s linear infinite;
        @keyframes active-2 {
          0%{
            height: 0;
          }50%{
            height: 80%;
          }100%{
            height: 0;
          }
        }
      }
      &:nth-child(3){
        animation: active-3 1s linear infinite;
        @keyframes active-3 {
          0%{
            height: 0;
          }50%{
            height: 80%;
          }100%{
            height: 0;
          }
        }
      }
    }
  }
}
</style>